#! /bin/bash

# debian/get-orig-source
# Part of the ‘lojban-common’ package.
#
# Copyright © 2005–2013 Ben Finney <ben+debian@benfinney.id.au>
# This is free software; you may copy, modify, and/or distribute this work
# under the terms of the GNU General Public License, version 3 or later.
# No warranty expressed or implied.
# See the file ‘/usr/share/common-licenses/GPL-3’ for details.

set -o errexit
set -o errtrace
set -o nounset

PACKAGE_NAME=lojban-common
PACKAGE_VERSION=$(
        dpkg-parsechangelog --count 1 \
        | grep '^Version: ' \
        | cut -d ' ' -f 2 \
        | sed -e 's/-[^-]\+$//' \
        )

LOJBAN_FILES_URL=http://www.lojban.org/publications
upstream_urls=(
        ${LOJBAN_FILES_URL}/wordlists/gismu.txt
        ${LOJBAN_FILES_URL}/wordlists/cmavo.txt
        ${LOJBAN_FILES_URL}/wordlists/rafsi.txt
        ${LOJBAN_FILES_URL}/wordlists/lujvo.txt
        )

function command_not_found_handle() {
    # Bash will (as per ‘bash(1)’ § “COMMAND EXECUTION”) invoke this
    # function on failure of a search through PATH for a command.
    local command="$1"
    local -a command_args="$@"

    case "$command" in
      wget | /usr/bin/wget)
        printf \
                "This program depends on the program ‘%s’, installed from the Debian package ‘%s’.\n" \
                "wget" "wget"
        ;;
      *)
        ;;
    esac

    printf "%s: %s: command not found\n" "$SHELL" "$command"
    return 127
}

program_dir="$(dirname $(readlink --canonicalize-existing $0))"

working_root=$(mktemp -t -d)
upstream_archive_name=${PACKAGE_NAME}-${PACKAGE_VERSION}
archive_working_dir=${working_root}/${upstream_archive_name}
mkdir ${archive_working_dir}

WGET_OPTS="--timestamping --directory-prefix ${archive_working_dir}"
wget ${WGET_OPTS} "${upstream_urls[@]}"

sha1sums_file="${program_dir}/upstream.sha1sums"
(
    cd ${archive_working_dir}
    sha1sum -c "${sha1sums_file}"
    )

TARBALL_SUFFIX=".tar.gz"
tarball_name=${PACKAGE_NAME}_${PACKAGE_VERSION}.orig${TARBALL_SUFFIX}
(
    cd ${working_root}
    tar -cf - ${upstream_archive_name}
    ) | gzip --best > ${tarball_name}

rm -r ${working_root}

# Local variables:
# coding: utf-8
# mode: sh
# End:
# vim: fileencoding=utf-8 filetype=sh :
